using System;
using System.Data;
using PCSComUtils.PCSExc;
using PCSComProduct.Costing.DS;

namespace PCSComProduct.Items.BO
{
	public class OperCostDetailBO //:  IOperCostDetailBO
	{
		private const string THIS = ".OperCostDetailBO";		
		public OperCostDetailBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IObjectBO Members

		public void UpdateDataSet(DataSet dstData)
		{
			// TODO:  Add OperCostDetailBO.UpdateDataSet implementation
		}

		public void Update(object pObjectDetail)
		{
			// TODO:  Add OperCostDetailBO.Update implementation
		}

		public void Delete(object pObjectVO)
		{
			// TODO:  Add OperCostDetailBO.Delete implementation
		}

		public void Add(object pObjectDetail)
		{
			// TODO:  Add OperCostDetailBO.Add implementation
		}

		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  Add OperCostDetailBO.GetObjectVO implementation
			return null;
		}

		#endregion
		//**************************************************************************              
		///    <Description>
		///    Get the Cost information
		///    1.Cost Description
		///    2. and the order cost
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///       Button will be disable or enable
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       THIENHD
		///    </Authors>
		///    <History>
		///       15-Dec-2004
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]		
		public DataTable GetListOfCostDetail(int pintProductID, int pintCCNID)
		{
			try 
			{
				//1.Get the ITM_CostDescription
				ITM_CostDescriptionDS objITM_CostDescriptionDS = new ITM_CostDescriptionDS();
				DataTable dtCost_Description = objITM_CostDescriptionDS.GetCostDescription(pintCCNID);

				//2.Get the ITM_CostOperation
				ITM_CostOperationDS objITM_CostOperationDS = new ITM_CostOperationDS();
				DataTable dtCostOperation = objITM_CostOperationDS.GetCostOperation(pintProductID,pintCCNID);

				//3.Define the column name for this table
				const string COSTDESCRIPTION_FLD = "CostDescription";
				const string COMPONENTCOST_FLD = "ComponentCost";
				const string VALUEADDEDCOST_FLD = "ValueAddedCost";
				const string ITEMCOST_FLD = "ItemCost";
				const string COSTDESC_PREFIX_FLD = "CostDesc";

				//Create this table
				DataTable dtResult = new DataTable();
				dtResult.Columns.Add(COSTDESCRIPTION_FLD);
				dtResult.Columns.Add(COMPONENTCOST_FLD);
				dtResult.Columns.Add(VALUEADDEDCOST_FLD);
				dtResult.Columns.Add(ITEMCOST_FLD);
				DataRow drNewRow;

				//Add default 21 empty row
				for (int i=1 ; i<=21; i++)
				{
					drNewRow = dtResult.NewRow();
					dtResult.Rows.Add(drNewRow);
				}

			/*
			For the CareSens, the demand for this is still not so much high because of the complexities of
			using this product.
			
			But if we can have good strategy for the first time, I think we will soon have good trademark
			
			According to our general research. for the first time
			the wholesale price would be 30.  
			
						 
			 
			 */
				bool blnHaveData = false;

				//first add the Cost Description to the 21 rows
				if (dtCost_Description.Rows.Count > 0)
				{
					blnHaveData = true;
					for (int i=1 ; i<=21; i++)
					{
						string strCostDescName = COSTDESC_PREFIX_FLD + i.ToString().PadLeft(2,'0');
						dtResult.Rows[i-1][COSTDESCRIPTION_FLD] = dtCost_Description.Rows[0][strCostDescName];
					}
				}

				//add the component cost, valued added cost, item cost
				if (dtCostOperation.Rows.Count > 0) 
				{
					blnHaveData = true;
					//assign the Component Cost
					for (int i=0; i<dtCostOperation.Columns.Count ; i++)
					{
						//get the column name
						string strColumnName = dtCostOperation.Columns[i].ColumnName;
						//get the last number of the column
						int intColumnSubfix  = int.Parse(strColumnName.Substring(strColumnName.Length - 2,2));
						//compare to know which column name comes to which field
						if (strColumnName.StartsWith("Com"))
						{
							dtResult.Rows[intColumnSubfix-1][COMPONENTCOST_FLD] = dtCostOperation.Rows[0][strColumnName];
						}
						else if (strColumnName.StartsWith("VAdd"))
						{
							dtResult.Rows[intColumnSubfix-1][VALUEADDEDCOST_FLD] = dtCostOperation.Rows[0][strColumnName];
						}
						else if (strColumnName.StartsWith("Oper"))
						{
							dtResult.Rows[intColumnSubfix-1][ITEMCOST_FLD] = dtCostOperation.Rows[0][strColumnName];
						}
					}

				}
				if (!blnHaveData)
				{
					dtResult.Rows.Clear();
				}
				
				
				return dtResult;
			}
			catch (PCSDBException ex) 
			{
				throw ex;
			}
			catch (Exception ex) 
			{
				throw ex;
			}

		}
	}
}
